package com.cloudkinto.service.channel.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.utils.dict.StaticDict;
import com.cloudkinto.dao.LcChannelRuleDao;
import com.cloudkinto.vo.lc.LcRuleDetailVo;
import com.cloudkinto.entity.LcChannelRuleDo;
import com.cloudkinto.service.channel.LcChannelRuleService;
import com.cloudkinto.vo.channel.lc.LcRuleDetailRes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author hua
 * @since 2024-10-29
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class LcChannelRuleServiceImpl extends ServiceImpl<LcChannelRuleDao, LcChannelRuleDo> implements LcChannelRuleService {
    @Autowired
    private LcChannelRuleDao dao;

    @Override
    public List<LcRuleDetailRes> getChannelRule(Long channelId) {
        List<LcRuleDetailVo> list = dao.getChannelRule(channelId);
        List<LcRuleDetailRes> resList = new ArrayList<>();
        for (LcRuleDetailVo et : list) {
            LcRuleDetailRes res = new LcRuleDetailRes();
            BeanUtils.copyProperties(et, res);
            StringBuilder builder = new StringBuilder();
            //介于
            if (Objects.equals(StaticDict.Rule_Operate_Sign.Between.getValue(), et.getOperateSign())) {
                builder.append(et.getStartValue()).append(this.getSizeText(et.getSizeUnit(), et.getCalculateType()));
                builder.append("<=").append(StaticDict.Channel_Calculate_Type.getText(et.getCalculateType())).append("<=").append(et.getEndValue());
                builder.append(this.getSizeText(et.getSizeUnit(), et.getCalculateType()));
            } else {
                builder.append(StaticDict.Channel_Calculate_Type.getText(et.getCalculateType())).append(StaticDict.Rule_Operate_Sign.getSign(et.getOperateSign())).append(et.getStartValue()).append(this.getSizeText(et.getSizeUnit(), et.getCalculateType()));
            }
            res.setRuleDetail(builder.toString());
            resList.add(res);
        }
        return resList;
    }

    public String getSizeText(String sizeUnit, int calculateType) {
        if (StaticDict.OMP_UnitMeasurement.In.getValue().equals(sizeUnit)) {
            return (StaticDict.Channel_Calculate_Type.getUnitType(calculateType) == 0 ? StaticDict.OMP_Unit.In.getValue() : StaticDict.OMP_Unit.Lb.getValue());
        } else {
            //公制单位
            return (StaticDict.Channel_Calculate_Type.getUnitType(calculateType) == 0 ? StaticDict.OMP_Unit.Cm.getValue() : StaticDict.OMP_Unit.Kg.getValue());
        }
    }
}
